from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time
import os
import sqlite3
import pathlib
delay=0.3
dss=('路瑞','15750192155','530621200801262049','7501','鲁甸县新街中学','胡炜','15287031673')
root=pathlib.Path(__file__).parent
db=sqlite3.connect(str(root/"gfjy.db"))
cur = db.cursor()
def init():
    driver = webdriver.Chrome()
    driver.set_window_size(480, 800)
    return driver
def SingUP(driver,ds):
    global db
    global cur
    driver.get('http://exam.zhishiyun.net/entry.html')

    # 定位到要悬停的元素
    above = driver.find_element_by_link_text("注 册")

    actions = ActionChains(driver)
    actions.move_to_element(above)
    actions.click(above)
    actions.perform()
    time.sleep(delay)
    while(driver.title!= '报名注册-云南省国防教育知识竞赛专用平台'):
        print(driver.title)
        time.sleep(delay)
    Select(driver.find_element_by_id('ddlGroup')).select_by_value("初中组")
    Select(driver.find_element_by_id('ddlGroupGrade')).select_by_value(ds[8])
    Select(driver.find_element_by_id('ddlArea')).select_by_value("昭通市")
    Select(driver.find_element_by_id('ddlCity')).select_by_value("鲁甸县")
    driver.find_element_by_id('txtAddress').send_keys(ds[7])
    driver.find_element_by_id('txtUserName').send_keys(ds[0])
    driver.find_element_by_id('txtMobile').send_keys(ds[1])
    driver.find_element_by_id('txtCardID').send_keys(ds[2])
    driver.find_element_by_id('txtSNo').send_keys(ds[3])
    driver.find_element_by_id('txtSchoolName').send_keys(ds[4])
    driver.find_element_by_id('txtTeacher').send_keys(ds[5])
    driver.find_element_by_id('txtTeacherPhone').send_keys(ds[6])
    su=driver.find_element_by_id('btnJoinExam')
    actions = ActionChains(driver)
    actions.move_to_element(su)
    actions.click(su)
    actions.perform()
    time.sleep(delay)
    while(driver.title!= '云南省国防教育知识竞赛专用平台'):
        print(driver.title)
        time.sleep(delay)
        try:
            above = driver.find_element_by_link_text("确定")
        except:
            pass
        else:
            actions = ActionChains(driver)
            actions.move_to_element(above)
            actions.click(above)
            actions.perform()
            break

    try:
        time.sleep(delay)
        cardID=driver.find_element_by_id('cardID')
        time.sleep(delay)
        time.sleep(delay)
        time.sleep(delay)
        driver.execute_script('signOut()')
    except :
        print("注册失败")
        sql="UPDATE `Sheet1` SET `注册` = '2' WHERE rowid = {0}".format(ds[3])
        cur.execute(sql)
    else:
        sql="UPDATE `Sheet1` SET `注册` = '1' WHERE rowid = {0}".format(ds[3])
        cur.execute(sql)
        print("注册成功")
    db.commit()
driver=init()

#dss=('路瑞','15750192155','530621200801262049','7501','鲁甸县新街中学','胡炜','15287031673')

sql="select `姓名`,`手机号码`,`身份证号`,`学号`,`学校`,`指导老师`,`指导老师电话`,`详细地址`,`班级` from sheet1 where `注册` = 0"
result=cur.execute(sql)
for i in result.fetchall():
    print(i)
    print(i[8])
    SingUP(driver,i)